package com.cff.anno.handler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import com.cff.anno.AutoDi;
import com.cff.anno.NettyComp;
import com.cff.anno.NettyServer;
import com.cff.netty.anno.self.update.NettyServerConfigeration;

@Component
public class SelfAnnotationHandler implements ApplicationContextAware{


	public Boolean isNettyServer(Class<?> bean){
		return AnnotationUtils.findAnnotation(bean, NettyServer.class) != null;
	}
	
	public Boolean isNettyComp(Class<?> bean){
		return AnnotationUtils.findAnnotation(bean, NettyComp.class) != null;
	}
	
	public void autoDi(Object bean, ApplicationContext tx) throws BeansException, IllegalArgumentException, IllegalAccessException, InstantiationException{
		System.out.println("准备为"+bean.toString() + "注入属性");
		Field[] fields = bean.getClass().getDeclaredFields();
		for (Field f : fields) {
			System.out.println("属性为："+f.getType());
			System.out.println("名称为："+f.getName());
			Annotation[] annos = f.getAnnotationsByType(Autowired.class);
			if(annos != null && annos.length >0){
				f.setAccessible(true);
				System.out.println("Autowired方法的属性为："+f.getType());
				System.out.println("Autowired方法的名称为："+f.getName());
				f.set(bean, tx.getBean(f.getName()));
				f.setAccessible(false);
				continue;
			}
			Annotation[] annos1 = f.getAnnotationsByType(AutoDi.class);
			if(annos1 != null && annos1.length >0){
				f.setAccessible(true);
				System.out.println("AutoDi方法的属性为："+f.getType());
				System.out.println("AutoDi方法的名称为："+f.getName());
				if(!isNettyComp(f.getType()))return;
				Object o = f.getType().newInstance();
				autoDi(o,tx);
				f.set(bean, o);
				f.setAccessible(false);	
				continue;
			}
		}
	}
	
	
	public void setApplicationContext(ApplicationContext tx) throws BeansException {
		String[] beanNames = tx.getBeanNamesForType(Object.class);
		for (String beanName : beanNames) {

			if(isNettyServer(tx.getType(beanName))){
				System.out.println("找到一个NettyServer：" + beanName);
				NettyServerConfigeration nettyServerConfigeration =  (NettyServerConfigeration) tx.getBean(beanName);
				try {
					autoDi(nettyServerConfigeration,tx);
				} catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("端口号："+AnnotationUtils.findAnnotation(nettyServerConfigeration.getClass(), NettyServer.class).port());
				String port = AnnotationUtils.findAnnotation(nettyServerConfigeration.getClass(), NettyServer.class).port();
				
				nettyServerConfigeration.setPort(Integer.parseInt(port));
				Thread t = new Thread(nettyServerConfigeration);
				t.start();
			}
		}
	}
	
}
